package cn.ffcs.bpmpvc.graph;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import cn.ffcs.bpmpvc.dao.FlowPvcDao;
import cn.ffcs.bpmpvc.graph.geom.Line;
import cn.ffcs.bpmpvc.graph.geom.LineDrawer;
import cn.ffcs.bpmpvc.graph.geom.Rect;

public class FlowPvcGraphService {
    /** buffer size. */
    public static final int BUFFER_SIZE = 1024;

    /** default pic size. */
    public static final int DEFAULT_PIC_SIZE = 48;

    /** default font size. */
    public static final int DEFAULT_FONT_SIZE = 12;

    /** rect offset. */
    public static final int RECT_OFFSET = 4;

    /** rect round. */
    public static final int RECT_ROUND = 20;

    /** r. */
    public static final int R = 27;
    /** g. */
    public static final int G = 118;

    /** b. */
    public static final int B = 164;

    /** default color. */
    public static final Color DEFAULT_COLOR = new Color(R, G, B);
    public static final Color DEFAULT_RUNNING_COLOR = Color.RED;

    /** default stroke. */
    public static final Stroke DEFAULT_STROKE = new BasicStroke(2);
    
    public void printGraph(String processId,FlowPvcDao flowPvcDao,OutputStream out,Map<String, Node> actMap){
        try {
        	List<Node> nodeList=flowPvcDao.findNodeList(processId);
        	List<Node> nexNodeList=flowPvcDao.findNexNodeList(processId);
            Rect picRect = getPicRect(nodeList);
            BufferedImage bi = new BufferedImage(picRect.getW(),picRect.getH(), BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2 = bi.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Font font = new Font("黑体", Font.PLAIN,DEFAULT_FONT_SIZE);
            
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            Rectangle2D r2=null;
            //Node node =null;
            String picName=null;
            Node obj=null;
            String name=null;
            String nodeId=null;
            Node toNode=null;
            Line line=null;
            double xx=0;
            double yy=0;
            Color fillColor=new Color(245,248,255);
            //r2 = font.getStringBounds(name, frc);
            g2.setStroke(DEFAULT_STROKE);
            g2.setColor(Color.RED);
            g2.drawString("注：红框所示为当前办理环节", 10, 10);
            for (Node node : nodeList) {
            	name = node.getName();
            	nodeId=node.getId();
                //picName = node.getPicName();
                if (picName != null) {
                    BufferedImage bi2 = ImageIO.read(this.getClass().getResourceAsStream(picName));
                    g2.drawImage(bi2, node.getX(), node.getY(), null);
                	//设置结束活动状态
                } else {
                	g2.setColor(DEFAULT_COLOR);
                    g2.setStroke(DEFAULT_STROKE);
                    //设置运行状态中活动的样式
                	if(actMap!=null&&actMap.size()>0){
                		obj=actMap.get(nodeId);
                		if(obj!=null){
                			g2.setColor(DEFAULT_RUNNING_COLOR);
                		}
                	}
                    
                    int x = node.getX() + RECT_OFFSET;
                    int y = node.getY() + RECT_OFFSET;
                    int w = node.getW() - (RECT_OFFSET * 2);
                    int h = node.getH() - (RECT_OFFSET * 2);
                    g2.drawRoundRect(x, y, w, h, RECT_ROUND, RECT_ROUND);
                    g2.setColor(fillColor);
                    g2.fillRoundRect(x+2, y+2, w-4, h-4, RECT_ROUND, RECT_ROUND);
                    //g2.fill3DRect(x, y, w, h, true);
                    //g2.draw3DRect(x, y, w, h, true);
                    
                    r2 = font.getStringBounds(name, frc);

                    xx = node.getX()+ ((node.getW() - r2.getWidth()) / 2);
                    yy = (node.getY()+ ((node.getH() - r2.getHeight()) / 2))- r2.getY();
                    g2.setColor(Color.BLACK);
                    g2.drawString(name, (int) xx, (int) yy);
                    
                }
                for (Node nextNode : nexNodeList) {
					if(node.getId().equals(nextNode.getParentId())){
						toNode = nextNode;
						line = node.getCrossLine(toNode);
						if (line != null) {
	                        LineDrawer drawer = new LineDrawer(line.getX1(),line.getY1(), line.getX2(), line.getY2());
	                        drawer.draw(g2, 1F);
	                    }
					}
				}
			}
            ImageIO.write(bi, "png", out);
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * 计算图片的大小.
     *
     * @param nodeMap Map
     * @return rect
     */
    public Rect getPicRect(List<Node> nodeList)  {
        Rect rect = new Rect();
        for (Node node : nodeList) {
            if ((node.getW()/2 + node.getX()) > rect.getW()) {
                rect.setW(node.getW()/2 + node.getX());
            }

            if ((node.getH() + node.getY()) > rect.getH()) {
                rect.setH(node.getH() + node.getY());
            }
		}
        rect.setW(rect.getW()*2);
        rect.setH(rect.getH() + 20);
        return rect;
    }
}
